package com.scada.controller;


import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.scada.contant.MessageConstants;
import com.scada.hessian.client.impl.UserVisitorClient;
import com.scada.model.bean.User;
import com.scada.model.bean.vo.UserRoleVO;
import com.scada.model.bean.vo.UserVO;
import com.scada.model.result.PageInfo;
import com.scada.model.result.ResponseBean;
import com.scada.service.UserService;


/** 
 * @ClassName: UserController 
 * @Description: 用户管理
 * @author: 黄胜
 * @date: 2016年11月9日 上午10:45:28  
 */
@Controller
@RequestMapping("user")
public class UserController extends BaseController{
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private UserVisitorClient userVisitorClient;
	
	public String initpassword;
	
	@RequestMapping(value = "toUser")
	public ModelAndView toMain(){
		
		return new ModelAndView("system/user");
	}
	
	/** 
	 * @FunctionName: listData
	 * @Description: 用户列表数据
	 * @author: 黄胜
	 * @date: 2016年12月6日 下午9:19:29 
	 * @param request
	 * @param pageInfo
	 * @param user
	 * @return 
	 */
	@RequestMapping(value = "listData")
	@ResponseBody
	public String listData(HttpServletRequest request,@ModelAttribute PageInfo<UserVO> pageInfo,
			@ModelAttribute User user){
		pageInfo.setPageSize(Integer.parseInt(request.getParameter("length")));
		user.setState(MessageConstants.LoginState.STATE_DEL.getKey());
		pageInfo.setCondition(user);
		pageInfo = userVisitorClient.findDataList(pageInfo);
		return toJson(pageInfo);
	}
	
	/** 
	 * @FunctionName: insert
	 * @Description: 新增用户
	 * @author: 黄胜
	 * @date: 2016年12月6日 下午7:34:56 
	 * @param user 对象用户
	 * @param roleIds 角色id字符串，用（,）隔开
	 * @return 
	 */
	@RequestMapping(value = "insert")
	public ModelAndView insert(User user,String roleIds){
		user.setPassword(DigestUtils.md5Hex(user.getPassword()));
		ResponseBean result = userVisitorClient.insert(user,roleIds);
		return result.getJsonModelView();
	}
	
	/** 
	 * @FunctionName: findUserDetail
	 * @Description: 获取用户详情信息
	 * @author: 黄胜
	 * @date: 2016年12月6日 下午9:26:09 
	 * @param uId
	 * @return 
	 */
	@RequestMapping(value = "findUserDetail")
	@ResponseBody
	public UserRoleVO findUserDetail(int uId){
		
		UserRoleVO urVO = userVisitorClient.findUserDetail(uId);
//	 	取出来的密码放到全局变量里，修改是判断密码是否更换了，
		initpassword =  urVO.getPassword();
		return urVO;
	}
	
	/** 
	 * @FunctionName: update
	 * @Description: 更新员工
	 * @author: 黄胜
	 * @date: 2016年12月6日 下午8:04:01 
	 * @param user
	 * @param roleIds 角色id字符串，用（,）隔开
	 * @return 
	 */
	@RequestMapping(value = "update")
	public ModelAndView update(User user,String roleIds){
//		判断密码是否更换
		if(StringUtils.isNotBlank(initpassword) && 
				!StringUtils.equals(initpassword, user.getPassword())){
			if(!StringUtils.equals(initpassword, DigestUtils.md5Hex(user.getPassword()))){
				user.setPassword(DigestUtils.md5Hex(user.getPassword()));
			}else{
				user.setPassword(null);
			}
		}
		ResponseBean result = userVisitorClient.update(user,roleIds);
		return result.getJsonModelView();
	}
	
	/** 
	 * @FunctionName: updateState
	 * @Description: 更新状态，启用禁用
	 * @author: 黄胜
	 * @date: 2016年12月6日 下午8:22:27 
	 * @param uIds
	 * @param method
	 * @return 
	 */
	@RequestMapping(value = "updateState")
	public ModelAndView updateState(String uIds,String method){
		
		ResponseBean result = userService.updateState(uIds,method);
		return result.getJsonModelView();
	}
	
	@RequestMapping(value = "ifPawAgreement.ajax")
	@ResponseBody
	public String ifPawAgreement(String oldPad){
		
		User user = getCurrentUser();
		if(StringUtils.equals(DigestUtils.md5Hex(oldPad), user.getPassword())){
			return "true";
		}else{
			return "false";
		}
		
	}
	
	@RequestMapping(value = "updatePassword.ajax")
	@ResponseBody
	public String updatePassword(String newPad){
		
		User user = getCurrentUser();
		return toJson(userService.updatePassword(newPad, user.getId()));
	}
	
	@RequestMapping(value = "whetherUsernameExist")
	@ResponseBody
	public String whetherUsernameExist(String loginName,String userId){
		
		return toJson(userVisitorClient.usernameExist(loginName,userId));
	}
}
